package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.PmCompany;
import com.example.demo.mapper.PmCompanyMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/pm_company")
public class PmCompanyController {
    @Resource
    PmCompanyMapper pmCompanyMapper;

    @PostMapping
    public synchronized Result<?> save(@RequestBody PmCompany pmCompany) {
        return Result.success(pmCompanyMapper.insert(pmCompany));
    }

    @PutMapping
    public synchronized Result<?> update(@RequestBody PmCompany pmCompany) {
        return Result.success(pmCompanyMapper.updateById(pmCompany));
    }

    @DeleteMapping("/{id}")
    public synchronized Result<?> delete(@PathVariable Long id) {
        return Result.success(pmCompanyMapper.deleteById(id));
    }

    @GetMapping("/{id}")
    public synchronized Result<?> get(@PathVariable Long id) {
        return Result.success(pmCompanyMapper.selectById(id));
    }

    @GetMapping("/list")
    public synchronized Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<PmCompany> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(PmCompany::getCompany, search);
        }
        Page<PmCompany> pmCompanyPage = pmCompanyMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(pmCompanyPage);
    }

    @GetMapping("/listAll")
    public synchronized Result<?> all() {
        return Result.success(pmCompanyMapper.selectList(null));
    }
}
